package com.lzb.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzb.reggie.common.CustomException;
import com.lzb.reggie.dto.SetMealDto;
import com.lzb.reggie.entity.SetMeal;
import com.lzb.reggie.entity.SetMealDish;
import com.lzb.reggie.mapper.SetMealMapper;
import com.lzb.reggie.service.SetMealDishService;
import com.lzb.reggie.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishService setMealDishService;

    /**
     * 新增套餐，同时保存套餐和菜品的关联关系
     * @param setMealDto
     */
    @Override
    public void saveSetMealDish(SetMealDto setMealDto) {

        this.save(setMealDto);

        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes().stream().map((item) -> {
            item.setSetmealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setMealDishes);

    }

    /**
     * 删除套餐以及套餐和菜品的关联关系
     * @param ids
     */
    @Override
    public void deleteSetMealDish(List<Long> ids) {

        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.in(SetMeal::getId, ids);
        setMealLambdaQueryWrapper.eq(SetMeal::getStatus, 1);

        int count = (int) this.count(setMealLambdaQueryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        this.removeBatchByIds(ids);
        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetMealDish::getSetmealId, ids);
        setMealDishService.remove(queryWrapper);

    }

    /**
     * 根据id查询套餐以及套餐和菜品的关联关系
     * @param id
     * @return
     */
    @Override
    public SetMealDto getByIdWithDish(Long id) {

        SetMeal setMeal = this.getById(id);
        SetMealDto setMealDto = new SetMealDto();
        BeanUtils.copyProperties(setMeal, setMealDto);

        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetmealId, id);
        List<SetMealDish> list = setMealDishService.list(queryWrapper);

        setMealDto.setSetmealDishes(list);

        return setMealDto;
    }

    /**
     * 修改套餐以及套餐和菜品的关联关系
     * @param setMealDto
     */
    @Override
    public void updateSetMealDish(SetMealDto setMealDto) {

        this.updateById(setMealDto);

        LambdaQueryWrapper<SetMealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetMealDish::getSetmealId, setMealDto.getId());
        setMealDishService.remove(queryWrapper);

        List<SetMealDish> setMealDishes = setMealDto.getSetmealDishes();
        setMealDishes = setMealDishes.stream().map((item) -> {
            item.setSetmealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setMealDishService.saveBatch(setMealDishes);
    }

    /**
     * 根据ids查询套餐信息
     * @param ids
     * @return
     */
    @Override
    public List<SetMeal> getByIds(List<Long> ids) {
        return setMealMapper.selectBatchIds(ids);
    }
}
